package com.bole.controller;

import com.bole.data.Result;
import com.bole.data.ResultEnum;
import com.bole.dto.specdetails.TbCarSpecDetailsDto;
import com.bole.dto.user.DicCarSpecDto;
import com.bole.feign.CarSpecDetailsFeign;
import com.bole.feign.CarSpecFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("details")
@Api(value="规格详情接口")
public class CarSpecDetailsController {
    @Autowired
    private CarSpecDetailsFeign carSpecDetailsFeign;
    @Autowired
    private CarSpecFeign carSpecFeign;

    @GetMapping("tolist")
    @ApiOperation(value = "跳转规格详情页面")
    public String tolist(Model model){
        List<DicCarSpecDto> list = carSpecFeign.getSpecInfo();
        model.addAttribute("list",list);

        return "car-spec-details";
    }

    @PostMapping("getSpecDetailsInfo")
    @ApiOperation(value = "查询规格详情信息")
    @ResponseBody
    public Result getSpecDetailsInfo(){
        List<TbCarSpecDetailsDto> list =  carSpecDetailsFeign.getSpecInfo();
        return Result.response(ResultEnum.OK).setData(list);
    }


    @PostMapping("saveSpecDetailsInfo")
    @ApiOperation(value = "增加规格详情")
    @ApiImplicitParam(name = "dicCarSpecDto",value = "规格详情",required = true,dataTypeClass = TbCarSpecDetailsDto.class)
    @ResponseBody
    public Result saveSpecInfo(@RequestBody TbCarSpecDetailsDto carSpecDetailsDto){
        carSpecDetailsFeign.saveSpecDetailsInfo(carSpecDetailsDto);
        return Result.response(ResultEnum.OK);
    }


    @PostMapping("toUpdate/{id}")
    @ApiOperation(value = "回显")
    @ApiImplicitParam(name = "id",value = "规格详情id",required = true,dataTypeClass = Long.class)
    @ResponseBody
    public Result toUpdate(@PathVariable("id")Long id){
        TbCarSpecDetailsDto carSpecDetailsDto = carSpecDetailsFeign.toUpdate(id);
        return Result.response(ResultEnum.OK).setData(carSpecDetailsDto);
    }


    @PostMapping("updateSpecDetailsInfo")
    @ApiOperation(value = "修改规格详情")
    @ApiImplicitParam(name = "dicCarSpecDto",value = "规格详情",required = true,dataTypeClass = DicCarSpecDto.class)
    @ResponseBody
    public Result updateSpecInfo(@RequestBody TbCarSpecDetailsDto carSpecDetailsDto){
        carSpecDetailsFeign.updateSpecInfo(carSpecDetailsDto);
        return Result.response(ResultEnum.OK);
    }



    @PostMapping("deleteBatch/{ids}")
    @ApiOperation(value = "批量删除")
    @ApiImplicitParam(name = "ids",value = "规格详情id",required = true,paramType = "path",dataTypeClass = Long.class)
    @ResponseBody
    public Result deleteBatch(@PathVariable("ids")Long[] ids){
        List<Long> ides = new ArrayList<>();
        for (Long id : ids) {
            ides.add(id);
        }
        carSpecDetailsFeign.deleteBatch(ides);
        return Result.response(ResultEnum.OK);
    }

}
